"""
be able to run the command
and return the error when error occur
"""
import sys
import os
from arff import *

commandclass ="""java weka.classifiers.functions.SMO -C 1.0 -L 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1 -K "weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1.0" -t classtrain.arff -d classchoosemodel -i -T classtest.arff > classresult.txt"""

class RunWekaError(Exception): pass
class TrainFileNotExistError(RunWekaError):pass
class TestFileNotExistError(RunWekaError):pass
class EssentialParamNotExistError(RunWekaError):pass


class Param():
    """
    design to be the parameter of the classifer
    """

def rawexecute(cmd):
    """
    execute the raw command 
    """
    os.system(cmd)

def SMODetail(params):
    """
    @param the trainfile and testfile -t -T >
    # user define 
    -t classtrain.arff
    -T classtest.arff
    > smodisclassresult.txt

    # get from the arff file
    -p should get here

    # default
    -i
    -M
    -distribution
    """
    cmd = "java weka.classifiers.functions.SMO -C 1.0 -L 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1 -K \"weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1.0\""
    defaultparams = {"-i":"","-M":"","-distribution":""}

    if (not params.has_key("-t")) or (not params.has_key("-T")) or (not params.has_key(">")):
        raise EssentialParamNotExistError("params not exist error")

    attrs = getAttrList(params["-t"])
    params["-p"] = str(len(attrs))

    for key in defaultparams:
        cmd += " "+key+" "+defaultparams[key]
    
    for key in params:
        if key == ">":
            continue
        cmd += " "+key+" "+params[key]
    cmd += " > "+params[">"]
    
    os.system(cmd)

def SVMWrapper(param):
    """
    run svm wrapper
    """
    pass

def SMOTrainTestWrapper(params):
    """
    run smo wrapper
    @param param should be dic of the parameters
    """
    """
    #default part
    defaultdic = {'-C':'1.0',
                  '-L':'0.0010',
                  '-P':'1.0E-12',
                  '-N':'0',
                  '-V':'-1',
                  '-W':'1',
                  '-K':"\"weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1.0\""}

     -C 1.0 -L 0.0010 -P 1.0E-12 -N 0 -V -1 -W 1 -K "weka.classifiers.functions.supportVector.PolyKernel -C 250007 -E 1.0"
     -t classtrain.arff -d classchoosemodel -i -T classtest.arff -p 1232 -M -distribution > smodisclassresult.txt             
    #user define part
    -d classchoosemodel
    -i
    
    -t classtrain.arff
    -T classtest.arff

    > classresult.txt
    
    basecmd = "java weka.classifiers.functions.SMO "
    cmd = basecmd
    
    cmd += " -T"
    if params.has_key('-T'):
        cmd += " " + params['-T']
    else:
        raise TestFileNotExistError("")

    for key in params.keys():
        cmd += " " + key
        cmd += " " + params[key]
        
    #how to get the output to the console
    os.system(cmd)

    -p 13
    > detail.txt
    """
    pass
    
def SMOTestWrapper(params):
    """
    just for test on the test set using the model already trained
    """
    
    """
    cmd += " -l"
    if params.has_key('-l'):
        cmd += " "+params['-l']
    else:
        cmd += " tmpclassifermodel"
    """
    pass
